package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yksj.healthtalk.entity.NewsEntity;
import com.yksj.healthtalk.ui.R;

public class NewsAdapter extends BaseAdapter {
	final List<NewsEntity> list = new ArrayList<NewsEntity>();
	final LayoutInflater inflater;

	public NewsAdapter(Context context, List<NewsEntity> list) {
		inflater = LayoutInflater.from(context);
		if (list != null)
			this.list.addAll(list);
		for (int i = 0; i < 10; i++) {
			NewsEntity newsEntity = new NewsEntity();
			newsEntity.setTime(new Date().toGMTString());
			newsEntity.setTitle("新闻" + i);
			this.list.add(newsEntity);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public NewsEntity getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public void addNews(List<NewsEntity> list) {
		list.addAll(list);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.news_item, null);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.new_title);
			TextView timeTextView = (TextView) convertView
					.findViewById(R.id.new_time);
			convertView.setTag(viewHolder);
			viewHolder.timeTextV = timeTextView;
			viewHolder.titleTextV = titleTextView;
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		NewsEntity newsEntity = getItem(position);
		viewHolder.timeTextV.setText(newsEntity.getTime());
		viewHolder.titleTextV.setText(newsEntity.getTitle());
		return convertView;
	}

	private static class ViewHolder {
		TextView timeTextV;
		TextView titleTextV;
	}
}
